<template>
  <navigationPageView
    :userInfo="userInfo"
    :appList="appList"
    :editPwddialog="editPwddialog"
    :addPwdform="addPwdform"
    @outSide="outSide"
    @getAppChild="getAppChild"
    @goBack="goBack"
    @editPwdClose="editPwdClose"
    @editPwdCancel="editPwdCancel"
    @editPwdKeep="editPwdKeep"
    ref="navigationP"
  ></navigationPageView>
</template>

<script>
  import navigationPageView from './navigationPageView';
  import {localUitl} from '@/utils/localUitl'

  export default {

    components: {
      navigationPageView
      // name
    },
    name: "navigationPageLogic",
    props: {},
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        userInfo: {},
        appList: [], // 传入数据
        totalAppList: [], // 总数据
        showAppList: [], // 显示数据
        editPwddialog: false,
        addPwdform: {
          oldPwd: "",
          newPwd: "",
          againPwd: ""
        }
      }
    },
    created() {
      this.getUserInfo();
      this.getClients()
    },
    mounted() {

    },
    methods: {
      // 根据access_token获取用户信息
      getUserInfo() {
        let obj = {
          access_token: localUitl.getToken()
        };
        _https.get.getUserN(obj).then(res => {
          if (res.status == 200) {
            this.userInfo = res.data;
          }
        })
      },
      /**
       * 点击退出登录
       * @param command 选择项
       */
      outSide(command) {
        switch (command) {
          case "editPwd":
            this.editpwd();
            break;
          case "goOut":
            localUitl.removeSessionItem("userData");
            this.$router.push('/');
            break;
        }
      },
      // 获取应用列表
      getClients(id) {
        let obj = {
          access_token: localUitl.getToken()
        };
        _https.get.getClients(obj).then(res => {
          if (res.status == 200) {
            if (res.data.length == 1 && res.data[0].childs == null) {
              if (res.data[0].build_type == "0") {
                this.$router.push({
                  path: '/frame',
                  query: {clientId: res.data[0].id, name: res.data[0].name, icon: res.data[0].icon}
                });
              } else if (res.data[0].build_type == "1") {
                if (res.data[0].registeredredirecturis.indexOf("?") == -1) {
                  window.open(`${res.data[0].registeredredirecturis}?access_token=${localUitl.getToken()}`)
                } else {
                  window.open(`${res.data[0].registeredredirecturis}&access_token=${localUitl.getToken()}`)
                }
              } else {
                this.$router.push({
                  path: '/frame',
                  query: {
                    clientId: res.data[0].id,
                    name: res.data[0].name,
                    build_type: res.data[0].build_type,
                    icon: res.data[0].icon
                  }
                });
              }
            }
            this.appList = res.data;
            this.totalAppList = res.data;
          }
        })
      },
      /**
       * 点击应用item获取childs
       * @param childInfo
       */
      getAppChild(childInfo) {
        if (childInfo.childs && childInfo.childs.length != 0) {
          this.showAppList.push(childInfo.childs);
          this.appList = this.showAppList[this.showAppList.length - 1];
        } else {
          if (childInfo.build_type == "0") {
            this.$router.push({
              path: '/frame',
              query: {clientId: childInfo.id, name: childInfo.name, icon: childInfo.icon}
            });
          } else if (childInfo.build_type == "1") {
            if (childInfo.registeredredirecturis.indexOf("?") == -1) {
              window.open(`${childInfo.registeredredirecturis}?access_token=${localUitl.getToken()}`)
            } else {
              window.open(`${childInfo.registeredredirecturis}&access_token=${localUitl.getToken()}`)
            }
          } else {
            this.$router.push({
              path: '/frame',
              query: {
                clientId: childInfo.id,
                name: childInfo.name,
                build_type: childInfo.build_type,
                icon: childInfo.icon
              }
            });
          }
          console.log("childInfo", childInfo);
          // 路由跳转
        }
      },
      // 返回
      goBack() {
        if (this.showAppList.length > 0) {
          this.showAppList.pop();
          if (this.showAppList.length == 0) {
            this.appList = this.totalAppList;
          } else {
            this.appList = this.showAppList[this.showAppList.length - 1];
          }
        } else {
          this.$message({
            message: '已经是最顶层了，不能再返回了~',
            type: 'warning'
          });
        }

      },
      // 修改密码
      editpwd() {
        this.editPwddialog = true;
      },
      // 修改密码关闭的回调
      editPwdClose() {
        this.editPwddialog = false;
        this.$refs.navigationP.$refs.editPwdform.resetFields();
      },
      // 弹窗点击取消
      editPwdCancel() {
        this.editPwddialog = false;
        this.$refs.navigationP.$refs.editPwdform.resetFields();
      },
      // 弹窗点击保存
      editPwdKeep() {
        this.$refs.navigationP.$refs.editPwdform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            let addPwdform = this.addPwdform;
            let userInfo = this.userInfo;
            if (addPwdform.newPwd != addPwdform.againPwd) {
              this.$message({
                type: 'error',
                message: '密码不一致!'
              });
              return
            }

            let obj = {
              loginname: userInfo.name,
              oldPwd: addPwdform.oldPwd,
              newPwd: addPwdform.newPwd
            };

            _https.post.editUserPwd(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({
                  type: 'success',
                  message: `${res.data.message}，请重新登录！`
                });
                this.editPwddialog = false;
                this.$refs.navigationP.$refs.editPwdform.resetFields();
                localUitl.removeSessionItem("userData");
                this.$router.push('/');
              } else {
                this.$message({
                  type: 'error',
                  message: `${res.data.message}!`
                });
              }
            });


          }
        })
      }

    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
